<template>
    <el-form inline class="demo-form-inline">
        <el-form-item label="角色名">
            <el-input v-model="query.name" placeholder="角色名" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" plain activity @click="queryRole">查询</el-button>
            <el-button type="primary" plain activity @click="reset">重置</el-button>
            <el-button type="primary" plain @click="addRole">新增</el-button>
        </el-form-item>
    </el-form>
    <el-divider />
    <el-table ref="table" border v-loading="loading"  :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index"  label="序号" width="100px" />
        <el-table-column align="center"  prop="name" label="角色名" />
        <el-table-column align="center"  prop="code" label="角色编码" />
        <el-table-column align="center"  prop="updateTime" label="更新时间" >
            <template #default="scope">
                {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
            </template>
        </el-table-column>
        <el-table-column align="center"  label="操作">
            <template #default="scope">
                <el-link title="配置" type="primary" @click="configRole(scope.row.id)">
                    <el-icon :size="20">
                    <Setting />
                </el-icon>
                </el-link>
                &nbsp;
                <el-link title="修改" type="primary" @click="editRole(scope.row.id)">
                    <el-icon :size="20">
                    <Edit />
                </el-icon>
                </el-link>
                &nbsp;
                <el-popconfirm title="删除？" @confirm="deleteRole(scope.row.id)"  @cancel="">
                    <template #reference>
                        <el-link type="danger" title="删除">
                            <el-icon :size="20">
                                <Delete />
                            </el-icon>
                        </el-link>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
        <el-pagination
                style="display: flex;align-items:center;justify-content:center;"
                v-model:currentPage="query.currentPage"
                v-model:page-size="query.pageSize"
                :page-sizes="[10, 20, 30]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="query.total"
                @size-change="queryRole"
                @current-change="queryRole"
        />
    <ModRole @close="data.modRole = false"  @reload="queryRole" :modRole="data.modRole" :type="data.type" :id="data.id" />
    <RoleToMenu ref="configMenu" :id="data.id" />
</template>

<script setup>
    import ModRole from '@/components/system/ModRole.vue'
    import RoleToMenu from '@/components/system/RoleToMenu.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'

    const configMenu = ref(null)

    let data = reactive({
        modRole:false,
        roleToMenu:false,
        tableData:[],
        type:"",
        id:"",
    });
    let loading = ref(true)
    let query = reactive({currentPage:1,pageSize:10,total:0})
    onMounted(() => {
        queryRole()
    })
    function queryRole(){
        request.post("/api/role/getRoles", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function reset (){
        query.name = ""
    }

    function addRole(){
        data.modRole = true
        data.type = "add"
    }

    function deleteRole(id){
        request.get(`/api/role/delete/${id}`).then(resp => {
            queryRole()
            ElMessage({message: '删除成功！',type: 'success'})
        });
    }

    function editRole(id){
        data.id = id
        data.type = "edit"
        data.modRole = true
    }

    function configRole(id){
        data.id = id
            configMenu.value.show()
    }

</script>
<style lang="css">

</style>